<template>
	<view class="page index">
		<view class="form">
			<common-input 
			  title="信用代码" 
				placeholder="请填写客户统一信用代码" 
				v-model="details.code"
				:border-bottom="false"
			>
			</common-input>
			<common-input
			  title="客户名称" 
				placeholder="请填写客户名称" 
				v-model="details.name"
				:border-bottom="false"
			>
			</common-input>
			<picker mode="selector" :value="typeIndex" :range="typeList" @change="selectType">
				<view class="picker">
					<common-input 
					  title="客户类型" 
					  placeholder="请选择客户类型" 
					  v-model="typeIndex < 0 ? '' : typeList[typeIndex]" 
					  :select="true" 
					  :disabled="true" 
					  :borderBottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="selector" :value="levelIndex" :range="levelList" @change="selectLevel">
				<view class="picker">
					<common-input 
					  title="客户等级" 
					  placeholder="请选择客户等级" 
					  v-model="levelIndex < 0 ? '' : levelList[levelIndex]" 
					  :select="true" 
					  :disabled="true" 
					  :borderBottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="region" @change="selectRegion">
				<view class="picker">
					<common-input
					  title="所在地区" 
						placeholder="省市区、县、乡镇等" 
						v-model="areaValue.length == 0 ? '' : areaValue[0] + areaValue[1] + areaValue[2]"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<common-input
			  title="详细地址" 
				placeholder="街道、楼牌号等" 
				v-model="details.detailAddress"
				:border-bottom="false"
			>
			</common-input>
			<common-input
			  title="联系电话" 
				placeholder="请输入联系电话" 
				v-model="details.telephone"
				:border-bottom="false"
				:require="false"
				:type="tel"
			>
			</common-input>
			<common-input
			  title="邮箱地址" 
				placeholder="请输入邮箱地址" 
				v-model="details.email"
				:require="false"
			>
			</common-input>
			<view class="text">
				<view class="title">
					<view>
						备注
					</view>
					<!-- <view class="require"></view> -->
				</view>
				<textarea v-model="details.remark" placeholder="请输入备注消息" placeholder-class="place"></textarea>
			</view>
		</view>
		<view class="footer">
			<button @click="save">保存</button>
		</view>
	</view>
</template>

<script>
	import commonInput from "@/components/commonInput.vue";
	import {updateCustomerApi} from "@/api/customer_api.js";
	export default{
		data() {
			return{
				typeIndex: 0,
				typeList: ['个体', '私营', '国营', '党政军', '境外'],
				levelIndex: 0,
				levelList: ['A（重要）', 'B（关注）', 'C（一般）'],
				areaValue:[],
				areaCode:'',
				details:{}
			}
		},
		components: {
			commonInput,
		},
		onLoad(e) {
			this.details = JSON.parse(e.details);
			this.typeIndex = this.details.type - 1;
			this.levelIndex = this.details.level -1;
			this.areaValue = this.details.areaName.split(' ');
			this.areaCode = this.details.areaId
		},
		methods:{
			selectType(e){
				let {detail:{value}} = e;
				this.typeIndex = value;
			},
			selectLevel(e){
				let {detail:{value}} = e;
				this.levelIndex = value;
			},
			// 选择地区
			selectRegion(e){
				console.log(e)
				this.areaValue = e.detail.value;
				this.areaCode = e.detail.code[2];
			},
			updatefiles(e){
				console.log(e,'===============')
			},
			save(){
				if(this.details.code == ''){
					uni.showToast({
						icon:'none',
						title:'信用代码不能为空'
					})
					return;
				}else if(this.details.name == ''){
					uni.showToast({
						icon:'none',
						title:'客户名称不能为空'
					})
					return;
				}else if(this.areaValue.length == 0){
					uni.showToast({
						icon:'none',
						title:'所在地区不能为空'
					})
					return;
				}else if(this.details.detailAddress == ''){
					uni.showToast({
						icon:'none',
						title:'详细地址不能为空'
					})
					return;
				}
				updateCustomerApi({
					id: this.details.id,
					code: this.details.code,
					name: this.details.name,
					type: Number(this.typeIndex) + 1,
					level: Number(this.levelIndex) + 1,
					areaId: this.areaCode,
					detailAddress: this.details.detailAddress,
					mobile: this.details.telephone,
					email: this.details.email,
					remark: this.details.remark
				}).then((res) => {
					uni.showToast({
						icon:'success',
						title:'编辑成功！'
					});
					setTimeout(() => {
						uni.navigateBack();
					},1500)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";
	.index{
		padding: 24rpx 0;
		@include flex-c;
		.form{
			flex: 1 0 0;
			overflow: auto;
			.text{
				border-bottom: 4rpx #e5e5e5 solid;
				padding: 12rpx 30rpx;
				.title{
					font-size: 28rpx;
					color: #101010;
					padding: 0 0 16rpx;
					display: flex;
					align-items: center;
					.require{
					  height: 28rpx;
					  width: 6rpx;
					  background-color: #c24135;
					  margin-left: 10rpx;
				  }
				}
				textarea{
					font-size: 28rpx;
					color: #909090;
					height: 125rpx;
				}
				.place{
					font-size: 28rpx;
					color: #909090;
				}
			}
			&::-webkit-scrollbar{
				display: none;
			}
		}
		.footer{
			flex: 0 0 auto;
			padding: 24rpx 130rpx;
			button{
				@include button;
				background-color: #198AFA;
				color: #fff;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 28rpx;
				width: 100%;
			}
		}
	}
</style>